package com.wteam.framework.common.utils;

import com.wteam.framework.modules.member.entity.entity.SdMember;
import com.wteam.framework.modules.member.mapper.mapper.SdMemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalTime;
import java.util.List;

@Component
public class MyScheduler {

    @Autowired
    private SdMemberMapper sdMemberMapper;

    // 每天12点触发任务
    @Scheduled(cron = "0 0 12 * * ?")
    public void refreshDatabaseField() {
        // 获取当前时间
        LocalTime currentTime = LocalTime.now();
        // 判断是否为12点
        if (currentTime.getHour() == 12) {
            // 获取需要更新的用户列表
            List<SdMember> members = sdMemberMapper.selectList(null);
            // 更新photoCount字段
            for (SdMember member : members) {
                //判断是否是会员
                if (!member.getIsMember()) {
                    //不是会员每天到12点自动更新
                    member.setPhotoCount(4L);
                }
                // 批量更新photoCount字段
                sdMemberMapper.updateBatchById(members);
            }
        }
    }
}
